package com.tz.entity;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

/**
 * Created by heziqian on 2016/10/21.
 */
@Entity
@Table(name = "ONLINE_ITEM")
public class OrderItem implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6198499296472411352L;
	private Long itemId;
	private Book book;
	private int count;
	private double totalPrice;
	private Order order;
	public OrderItem() {
		super();
	}
	
	public OrderItem(Long itemId, Book book, int count, double totalPrice,
			Order order) {
		super();
		this.itemId = itemId;
		this.book = book;
		this.count = count;
		this.totalPrice = totalPrice;
		this.order = order;
	}

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO, generator = "item_id")
	@SequenceGenerator(name = "item_id", sequenceName = "online_item_id")
	public Long getItemId() {
		return itemId;
	}
	public void setItemId(Long itemId) {
		this.itemId = itemId;
	}
	
	@ManyToOne
	@JoinColumn(name="book_id")
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	@Column(nullable=false)
	public int getCount() {
		return count;
	}
	public void setCount(int count) {
		this.count = count;
	}
	
	@Column(nullable=false)
	public double getTotalPrice() {
		return totalPrice;
	}

	public void setTotalPrice(double totalPrice) {
		this.totalPrice = totalPrice;
	}

	
	
	
	@ManyToOne(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
	@JoinColumn(name="order_id")
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((book == null) ? 0 : book.hashCode());
		result = prime * result + count;
		result = prime * result + ((itemId == null) ? 0 : itemId.hashCode());
		result = prime * result + ((order == null) ? 0 : order.hashCode());
		long temp;
		temp = Double.doubleToLongBits(totalPrice);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		OrderItem other = (OrderItem) obj;
		if (book == null) {
			if (other.book != null)
				return false;
		} else if (!book.equals(other.book))
			return false;
		if (count != other.count)
			return false;
		if (itemId == null) {
			if (other.itemId != null)
				return false;
		} else if (!itemId.equals(other.itemId))
			return false;
		if (order == null) {
			if (other.order != null)
				return false;
		} else if (!order.equals(other.order))
			return false;
		if (Double.doubleToLongBits(totalPrice) != Double
				.doubleToLongBits(other.totalPrice))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "OrderItem [itemId=" + itemId + ", book=" + book + ", count="
				+ count + ", totalPrice=" + totalPrice + "]";
	}
	
	
	
	
}
